<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;
use backend\models\FactorySearch;
use yii\grid\GridView;
use yii\helpers\Html;
use backend\models\Factory;
use yii\base\Object;
/**
 * 工厂控制器
 * @author suiyue
 *
 */
class FactoryController extends BaseController
{
	/**
	 * (non-PHPdoc)行为
	 * @see \yii\base\Component::behaviors()
	 */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * (non-PHPdoc)前置函数
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	$action_list = array(
    			'index',
    			'update',
    			'view',
    			'create',
    			'upload'
    	);
    	 $access=$this->checkAccess($action_list,$action);//判断操作的action是否在actionlist里面
    	 $this->beforeOp('factory', $access);//身份识别 权限验证  action验证
    	 return true;
    }
	/**
	 * 售后订单列表
	 */
    public function actionIndex()
    {
    	$searchModel = new FactorySearch();
    	$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    	return $this->render('index', [
    			'searchModel' => $searchModel,
    			'dataProvider' => $dataProvider,
    	]);
    }
    /**
     * 创建
     */
    public function actionCreate(){
    	$this->layout = 'main_small';
    	$model = new Factory();
    	if ($model->load(Yii::$app->request->post())) {
    		$short_code=$model->getRandomStrNum();
    		while($model->CheckFactoryByCode($short_code)){
    			$short_code=$model->getRandomStrNum();
    			if(!$model->CheckFactoryByCode($short_code)){
    				break;
    			}
    		}
    		if($model->CheckFactoryByCode($short_code)){
    			$model->short_code=$short_code;
    		}else{
    			$short_code=$model->getRandomStrNum();
    			$model->short_code=$short_code;
    		}
    		 	$model->save();
    		 	return $this->redirect(['view', 'id' => $model->id]);
    	} else {
    		return $this->render('create', [
    				'model' => $model,
    		]);
    	}
    }
    
    /**
     * 修改
     */
    public function actionUpdate($id){
    	$this->layout = 'main_small';
    	$model = $this->findModel($id);
    	if ($model->load(Yii::$app->request->post())) {
	    		$model->save();
	    		return $this->redirect(['view', 'id' => $model->id]);
    	} else {
    		return $this->render('update', [
    				'model' => $model
    		]);
    	}
    }
    
    /**
     * 查看单条数据
     */
    public function actionView($id){
    	$this->layout = 'main_small';
    	$model = Factory::findOne($id);
    	return $this->render('view', [
    			'model' => $model
    	]);
    }
    /**
     * 查找单个
     * @param   $id
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = Factory::findOne($id)) !== null) {
        	$model = Factory::find()->where('id='.$id)->one();
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * 百度编辑器(non-PHPdoc)
     * @see \yii\base\Controller::actions()
     */
    public function actions(){
    	return [
    			'upload'=>[
    					'class'=>'backend\widgets\ueditor\UEditorAction',
    					'config'=>[
    							//  /gjj/backend/web/images/
    							'imageUrlPrefix'=>"http://".$_SERVER['SERVER_NAME']."/gjj/backend/web/images/",//图片访问路径前缀
    							'imagePathFormat'=>'{rand:2}/{time}{rand:6}',
    							'imageRoot'=>\Yii::getAlias("images")
    					]
    			]
    	];
    
    }
    
}
